package hr.bean;

import java.util.ArrayList;
import java.util.Date;

import hr.dao.AttendanceDAO;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

import resources.pojo.hr_attendance;
import resources.pojo.hr_attendance_type;
import resources.pojo.hr_list_attendance;
import resources.util.Tool;

@ManagedBean(name="attendance")
@SessionScoped
public class Attendance {
	private int currentPage;
	private hr_attendance attendance;

	private hr_attendance selectedAttendance;
	private Date dateSearch;
	private String nameSearch;
	private String departmentSearch;

	private ArrayList<hr_list_attendance> listAttendance;
	private Integer[] pages;
	
	public void onload() {
		if(!Tool.isPostBack()){
			if(dateSearch!=null){
				listAttendance = AttendanceDAO.getListAttendancePaging(dateSearch, departmentSearch, nameSearch,1, Tool.max_row);
				setPages(Tool.getPages());
			}
		}
	}
	
	public void onloadUpdate() {
		if(!Tool.isPostBack()){
		}
	}
	
	public void onloadNew() {
		if(!Tool.isPostBack()){
			if(attendance == null){
				attendance = new hr_attendance();
				attendance.setType(new hr_attendance_type());
			}
		}
	}
	
	public void init(){
		currentPage = 1;
		listAttendance = AttendanceDAO.getListAttendancePaging(dateSearch, departmentSearch, nameSearch,currentPage, Tool.max_row);
		setPages(Tool.getPages());
	}
	


	public void paging(){
		setPages(Tool.getPages());
	}
	
	public void insert(){
		attendance.setId("ATT" + Tool.generateID());
		attendance.setStatus(1);
		String result = AttendanceDAO.action("insert",attendance);
		if(result.equals("completed")){
			Tool.redirect("attendance.html");
		}else if(result.equals("duplicate")){
			Tool.message("Bị trùng");
		}
	}
	
	public void update(){
		String result = AttendanceDAO.action("update",attendance);
		if(result.equals("completed")){
			Tool.redirect("attendance.html");
		}else if(result.equals("duplicate")){
			Tool.message("Bị trùng");
		}
	}
	
	public void accept(){
		attendance.setStatus(2);
		String result = AttendanceDAO.action("update",attendance);
		if(result.equals("completed")){
			Tool.redirect("attendance.html");
		}else if(result.equals("duplicate")){
			Tool.message("Bị trùng");
		}
	}
	
	public void cancel(){
		attendance.setStatus(3);
		String result = AttendanceDAO.action("update",attendance);
		if(result.equals("completed")){
			Tool.redirect("attendance.html");
		}else if(result.equals("duplicate")){
			Tool.message("Bị trùng");
		}
	}
	
	public void restore(){
		attendance.setStatus(1);
		String result = AttendanceDAO.action("update",attendance);
		if(result.equals("completed")){
			Tool.redirect("attendance.html");
		}else if(result.equals("duplicate")){
			Tool.message("Bị trùng");
		}
	}
	
	public void choose(ActionEvent event){
		hr_list_attendance temp = (hr_list_attendance) event.getComponent().getAttributes().get("object");
		if(temp.getAttendance()!=null){
			attendance = temp.getAttendance();
			attendance.setEmployee(temp.getEmployee());
			Tool.redirect("update-attendance.html");
		}else{
			attendance = new hr_attendance();
			attendance.setDate(dateSearch);
			attendance.setEmployee(temp.getEmployee());
			attendance.setType(new hr_attendance_type());
			Tool.redirect("new-attendance.html");
		}
	}
	
	public void search(){
		listAttendance = AttendanceDAO.getListAttendancePaging(dateSearch, departmentSearch, nameSearch,1, Tool.max_row);
		setPages(Tool.getPages());
	}
	
	
	public hr_attendance getAttendance() {
		return attendance;
	}

	public void setAttendance(hr_attendance attendance) {
		this.attendance = attendance;
	}

	public hr_attendance getSelectedAttendance() {
		return selectedAttendance;
	}

	public void setSelectedAttendance(hr_attendance selectedAttendance) {
		this.selectedAttendance = selectedAttendance;
	}

	public ArrayList<hr_list_attendance> getListAttendance() {
		return listAttendance;
	}

	public void setListAttendance(ArrayList<hr_list_attendance> listAttendance) {
		this.listAttendance = listAttendance;
	}
	public void nextPage(){
		currentPage ++;
	}

	public String getNameSearch() {
		return nameSearch==null?"":nameSearch;
	}

	public void setNameSearch(String nameSearch) {
		this.nameSearch = nameSearch;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public void setPages(Integer[] pages) {
		this.pages = pages;
	}

	public Integer[] getPages() {
		return pages;
	}


	public String getDepartmentSearch() {
		return departmentSearch==null?"":departmentSearch;
	}

	public void setDepartmentSearch(String departmentSearch) {
		this.departmentSearch = departmentSearch;
	}

	public Date getDateSearch() {
		return dateSearch;
	}

	public void setDateSearch(Date dateSearch) {
		this.dateSearch = dateSearch;
	}
}
